package com.example.homework8.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.homework8.R;
import com.example.homework8.bean.TagBean;

import java.util.ArrayList;
import java.util.List;

public class TagAdapter extends RecyclerView.Adapter<TagAdapter.ViewHolder> {

    private List<TagBean> mList = new ArrayList<>();
    private OnDelClickListener onDelClickListener;
    private int tag;

    public TagAdapter(int tag){
        this.tag = tag;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_tag,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") final int position) {
        holder.text.setText(mList.get(position).getText());
        if (tag == 1){

            holder.ivDel.setVisibility(View.GONE);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onDelClickListener.onItemClick(position,mList.get(position));
                }
            });
        }
        holder.ivDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onDelClickListener.onDelClick(position,mList.get(position));
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public void setData(List<TagBean> mList){
        this.mList.clear();
        this.mList.addAll(mList);
        if (tag == 1){
            TagBean bean = new TagBean();
            bean.setId(-3);
            bean.setText("全部");
            this.mList.add(0,bean);
        }
        notifyDataSetChanged();
    }

    public void remove(int position){
        this.mList.remove(position);
        notifyDataSetChanged();
    }

    public interface OnDelClickListener{
        void onDelClick(int position, TagBean bean);

        void onItemClick(int position, TagBean bean);
    }

    public void addOnDelClickListener(OnDelClickListener onDelClickListener){
        this.onDelClickListener = onDelClickListener;
    }

    static class ViewHolder extends RecyclerView.ViewHolder{

        private TextView text;
        private ImageView ivDel;

        public ViewHolder(View itemView) {
            super(itemView);
            text = itemView.findViewById(R.id.text);
            ivDel = itemView.findViewById(R.id.ivDel);
        }
    }
}
